﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Media;
using SilverChart.Core.Service;

namespace SilverChart.Core.Configuration.Converter {
    [ServiceProvider(LayzeCreation = true, Singleton = true)]
    public class NamedColorConverter : ConfigurationConverter<Color> {
        public const String COLOR = @"(?<NAMED_COLOR>[a-zA-Z]{3,20})";

        const String FULL_COLOR_REGEX = "^" + COLOR + "$";
        private static IDictionary<String, String> colors;
        private static Regex Named_Color_Regex_Object;
        private static ColorConverter converter;

        static NamedColorConverter() {
            Named_Color_Regex_Object = new Regex(FULL_COLOR_REGEX);
            converter = new ColorConverter();
            colors = new Dictionary<String, String>();
            colors["ALICEBLUE"] = "#F0F8FF";
            colors["ANTIQUEWHITE"] = "#FAEBD7";
            colors["AQUA"] = "#00FFFF";
            colors["AQUAMARINE"] = "#7FFFD4";
            colors["AZURE"] = "#F0FFFF";
            colors["BEIGE"] = "#F5F5DC";
            colors["BISQUE"] = "#FFE4C4";
            colors["BLACK"] = "#000000";
            colors["BLANCHEDALMOND"] = "#FFEBCD";
            colors["BLUE"] = "#0000FF";
            colors["BLUEVIOLET"] = "#8A2BE2";
            colors["BROWN"] = "#A52A2A";
            colors["BURLYWOOD"] = "#DEB887";
            colors["CADETBLUE"] = "#5F9EA0";
            colors["CHARTREUSE"] = "#7FFF00";
            colors["CHOCOLATE"] = "#D2691E";
            colors["CORAL"] = "#FF7F50";
            colors["CORNFLOWERBLUE"] = "#6495ED";
            colors["CORNSILK"] = "#FFF8DC";
            colors["CRIMSON"] = "#DC143C";
            colors["CYAN"] = "#00FFFF";
            colors["DARKBLUE"] = "#00008B";
            colors["DARKCYAN"] = "#008B8B";
            colors["DARKGOLDENROD"] = "#B8860B";
            colors["DARKGRAY"] = "#A9A9A9";
            colors["DARKGREEN"] = "#006400";
            colors["DARKKHAKI"] = "#BDB76B";
            colors["DARKMAGENTA"] = "#8B008B";
            colors["DARKOLIVEGREEN"] = "#556B2F";
            colors["DARKORANGE"] = "#FF8C00";
            colors["DARKORCHID"] = "#9932CC";
            colors["DARKRED"] = "#8B0000";
            colors["DARKSALMON"] = "#E9967A";
            colors["DARKSEAGREEN"] = "#8FBC8B";
            colors["DARKSLATEBLUE"] = "#483D8B";
            colors["DARKSLATEGRAY"] = "#2F4F4F";
            colors["DARKTURQUOISE"] = "#00CED1";
            colors["DARKVIOLET"] = "#9400D3";
            colors["DEEPPINK"] = "#FF1493";
            colors["DEEPSKYBLUE"] = "#00BFFF";
            colors["DIMGRAY"] = "#696969";
            colors["DODGERBLUE"] = "#1E90FF";
            colors["FIREBRICK"] = "#B22222";
            colors["FLORALWHITE"] = "#FFFAF0";
            colors["FORESTGREEN"] = "#228B22";
            colors["FUCHSIA"] = "#FF00FF";
            colors["GAINSBORO"] = "#DCDCDC";
            colors["GHOSTWHITE"] = "#F8F8FF";
            colors["GOLD"] = "#FFD700";
            colors["GOLDENROD"] = "#DAA520";
            colors["GRAY"] = "#808080";
            colors["GREEN"] = "#008000";
            colors["GREENYELLOW"] = "#ADFF2F";
            colors["HONEYDEW"] = "#F0FFF0";
            colors["HOTPINK"] = "#FF69B4";
            colors["INDIANRED"] = "#CD5C5C";
            colors["INDIGO"] = "#4B0082";
            colors["IVORY"] = "#FFFFF0";
            colors["KHAKI"] = "#F0E68C";
            colors["LAVENDER"] = "#E6E6FA";
            colors["LAVENDERBLUSH"] = "#FFF0F5";
            colors["LAWNGREEN"] = "#7CFC00";
            colors["LEMONCHIFFON"] = "#FFFACD";
            colors["LIGHTBLUE"] = "#ADD8E6";
            colors["LIGHTCORAL"] = "#F08080";
            colors["LIGHTCYAN"] = "#E0FFFF";
            colors["LIGHTGOLDENRODYELLOW"] = "#FAFAD2";
            colors["LIGHTGREEN"] = "#90EE90";
            colors["LIGHTGRAY"] = "#D3D3D3";
            colors["LIGHTPINK"] = "#FFB6C1";
            colors["LIGHTSALMON"] = "#FFA07A";
            colors["LIGHTSEAGREEN"] = "#20B2AA";
            colors["LIGHTSKYBLUE"] = "#87CEFA";
            colors["LIGHTSLATEGRAY"] = "#778899";
            colors["LIGHTSTEELBLUE"] = "#B0C4DE";
            colors["LIGHTYELLOW"] = "#FFFFE0";
            colors["LIME"] = "#00FF00";
            colors["LIMEGREEN"] = "#32CD32";
            colors["LINEN"] = "#FAF0E6";
            colors["MAGENTA"] = "#FF00FF";
            colors["MAROON"] = "#800000";
            colors["MEDIUMAQUAMARINE"] = "#66CDAA";
            colors["MEDIUMBLUE"] = "#0000CD";
            colors["MEDIUMORCHID"] = "#BA55D3";
            colors["MEDIUMPURPLE"] = "#9370DB";
            colors["MEDIUMSEAGREEN"] = "#3CB371";
            colors["MEDIUMSLATEBLUE"] = "#7B68EE";
            colors["MEDIUMSPRINGGREEN"] = "#00FA9A";
            colors["MEDIUMTURQUOISE"] = "#48D1CC";
            colors["MEDIUMVIOLETRED"] = "#C71585";
            colors["MIDNIGHTBLUE"] = "#191970";
            colors["MINTCREAM"] = "#F5FFFA";
            colors["MISTYROSE"] = "#FFE4E1";
            colors["MOCCASIN"] = "#FFE4B5";
            colors["NAVAJOWHITE"] = "#FFDEAD";
            colors["NAVY"] = "#000080";
            colors["OLDLACE"] = "#FDF5E6";
            colors["OLIVE"] = "#808000";
            colors["OLIVEDRAB"] = "#6B8E23";
            colors["ORANGE"] = "#FFA500";
            colors["ORANGERED"] = "#FF4500";
            colors["ORCHID"] = "#DA70D6";
            colors["PALEGOLDENROD"] = "#EEE8AA";
            colors["PALEGREEN"] = "#98FB98";
            colors["PALETURQUOISE"] = "#AFEEEE";
            colors["PALEVIOLETRED"] = "#DB7093";
            colors["PAPAYAWHIP"] = "#FFEFD5";
            colors["PEACHPUFF"] = "#FFDAB9";
            colors["PERU"] = "#CD853F";
            colors["PINK"] = "#FFC0CB";
            colors["PLUM"] = "#DDA0DD";
            colors["POWDERBLUE"] = "#B0E0E6";
            colors["PURPLE"] = "#800080";
            colors["RED"] = "#FF0000";
            colors["ROSYBROWN"] = "#BC8F8F";
            colors["ROYALBLUE"] = "#4169E1";
            colors["SADDLEBROWN"] = "#8B4513";
            colors["SALMON"] = "#FA8072";
            colors["SANDYBROWN"] = "#F4A460";
            colors["SEAGREEN"] = "#2E8B57";
            colors["SEASHELL"] = "#FFF5EE";
            colors["SIENNA"] = "#A0522D";
            colors["SILVER"] = "#C0C0C0";
            colors["SKYBLUE"] = "#87CEEB";
            colors["SLATEBLUE"] = "#6A5ACD";
            colors["SLATEGRAY"] = "#708090";
            colors["SNOW"] = "#FFFAFA";
            colors["SPRINGGREEN"] = "#00FF7F";
            colors["STEELBLUE"] = "#4682B4";
            colors["TAN"] = "#D2B48C";
            colors["TEAL"] = "#008080";
            colors["THISTLE"] = "#D8BFD8";
            colors["TOMATO"] = "#FF6347";
            colors["TURQUOISE"] = "#40E0D0";
            colors["VIOLET"] = "#EE82EE";
            colors["WHEAT"] = "#F5DEB3";
            colors["WHITE"] = "#FFFFFF";
            colors["WHITESMOKE"] = "#F5F5F5";
            colors["YELLOW"] = "#FFFF00";
            colors["YELLOWGREEN"] = "#9ACD32";

            
            colors["TRANSPARENT"] = "#00FFFFFF";
        }

        public bool Convert(string input, out Color value) {
            if (input != null && input.Trim().Length > 0 &&
                Named_Color_Regex_Object.IsMatch(input) && colors.ContainsKey(input.ToUpper())) {
                String color = colors[input.ToUpper()];
                return converter.Convert(color, out value);
            }

            value = Colors.Black;
            return false;
        }
    }
}
